package com.yloveg.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.collections4.map.HashedMap;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yloveg.base.BaseController;
import com.yloveg.base.Md5Util;
import com.yloveg.entity.ProductContent;
import com.yloveg.entity.SPermission;
import com.yloveg.entity.ShopInfo;
import com.yloveg.entity.ShopOrders;
import com.yloveg.entity.Suser;
import com.yloveg.entity.UserInfo;
import com.yloveg.entity.VPayOrder;
import com.yloveg.service.ProductContentService;
import com.yloveg.service.SPermissionService;
import com.yloveg.service.ShopOrdersService;
import com.yloveg.service.UserInfoService;
import com.yloveg.service.UserService;
import com.yloveg.service.VActivityService;
import com.yloveg.service.VComplainService;
import com.yloveg.service.VPayOrderService;
import com.yloveg.service.VVisitorLogService;


@Controller
@RequestMapping({"/","main"})  
public class MainController extends BaseController {
    
    @Autowired
    private UserInfoService userInfoService;
    
    @Autowired
    private UserService UserService;

    @Autowired
    private SPermissionService sPermissionService;
    
    @Autowired
    private ShopOrdersService shopOrdersService;
    
    @Autowired
    private ProductContentService productContentService;
    
    @Autowired
    private VActivityService vActivityService;

    @Autowired
    private VComplainService vComplainService;
    
    @Autowired
    private VVisitorLogService vVisitorLogService;
    
    @Autowired
    private VPayOrderService vPayOrderService;
    
    /**
     * 后台框架首页
     * @return
     */
    @RequestMapping({"/","index"})  
    public String index(ModelMap map){
    	map.put("userName", this.getSessionUser().getUsername());
    	UserInfo info = new UserInfo();
    	info.setUserId(this.getSessionUser().getId());
    	info = userInfoService.queryOneUserInfo(info);
    	if(info==null) {
    		map.put("info", new UserInfo());
    	}else {
    		map.put("info", info);
    	}
    	List<SPermission> mainMum =sPermissionService.getUserMainMum(this.getSessionUser().getId());
    	map.put("mainMum", mainMum);
        return "landing/index";
    }
    /**
     * 系统内容首页
     * @return
     */
    @RequestMapping("/home")
    public String home(ModelMap map){
    	ShopOrders record = new ShopOrders();
    	ProductContent product = new ProductContent(); 
    	ShopInfo shop = this.getSessionShop();
    	if(shop!=null&&shop.getId()!=null){
    		record.setShopId(shop.getId());
    		product.setShopId(shop.getId());
    	}else{
    		record.setShopId(1100000l);
    		product.setShopId(1100000l);
    	}
    	map.put("productCount", productContentService.queryCount(product));
    	map.put("count", shopOrdersService.queryCount(record));
    	map.put("activityCount", vActivityService.getActivityTotal());
    	map.put("activityOpen", vActivityService.getActivityOpen());
    	map.put("complainCount", vComplainService.getComplainCount());
    	map.put("visityDayCount", vVisitorLogService.selectByTodayCount());
    	map.put("payDayCount", vPayOrderService.selectByTodaySum(new VPayOrder()));
    	map.put("payCount", vPayOrderService.selectSum(new VPayOrder()));
        return "landing/content/home";
    }
    /**
     * 关于我们
     * @return
     */
    @RequestMapping("/aboutUs")
    public String aboutUs(){
        return "landing/content/aboutUs";
    }    
    /**
     * 个人信息
     * @return6
     */
    @RequestMapping("/perinfo")
    public String perinfo(ModelMap map){
    	UserInfo info = new UserInfo();
    	info.setUserId(this.getSessionUser().getId());
    	info = userInfoService.queryOneUserInfo(info);
    	if(info==null) {
    		map.put("info", new UserInfo());
    	}else {
    		map.put("info", info);
    	}
    	map.put("userName", this.getSessionUser().getUsername());
    	return "landing/perinfo";
    }
    /**
     * 根据id查询个人信息
     * @param info
     * @return
     */
    @RequestMapping("/queryPerinfo")
    @ResponseBody
    public Map<String, Object> queryPerinfo (Long userId) {
    	Map<String, Object> map = new HashedMap<String, Object>();
    	UserInfo info = new UserInfo();
    	Boolean result = false;
    	if(userId!=null){
    		info.setUserId(userId);
        	info = userInfoService.queryOneUserInfo(info);
        	if(info!=null){
        		result = true;
        		map.put("info", info);
        	}
    	}
    	if(result){
			map.put("code", "1");
			map.put("msg", "查询成功！");
    	}else {
			map.put("code", "0");
			map.put("msg", "系统错误请联系管理员！");
    	}
    	return map;
    }    
    
    /**
     * 个人信息
     * @return6
     */
    @RequestMapping("/changePwd")
    public String changePwd(ModelMap map){
    	map.put("userName", this.getSessionUser().getUsername());
    	return "landing/changePwd";
    }
    /**
     * 保存个人信息
     * @param info
     * @return
     */
    @RequestMapping("/saveUpUserInfo")
    @ResponseBody
    public Map<String, Object> saveUpUserInfo (UserInfo info) {
    	Map<String, Object> map = new HashedMap<String, Object>();
    	int result = 0;
    	if(info.getId()==0) {
    		info.setUserId(this.getSessionUser().getId());
    		result = userInfoService.insertUserInfo(info);
    	}else {
    		result = userInfoService.updateUserInfo(info);
    	}
    	if(result>0){
			map.put("code", "1");
			map.put("msg", "更新成功！");
    	}else {
			map.put("code", "0");
			map.put("msg", "系统错误请联系管理员！");
    	}
    	return map;
    }
    
	/**
	 * 更改密码
	* @param info
	* @return
	*/
	@RequestMapping("/changePwdSave")
	@ResponseBody
	public Map<String, Object> changePwdSave (String passwordOld,String passwordNew) {
		Map<String, Object> map = new HashedMap<String, Object>();
		Suser user = UserService.findUserByNameAndPassword(this.getSessionUser().getUsername());
		if(user.getPassword().equals(Md5Util.encode(passwordOld))){
			user.setPassword(Md5Util.encode(passwordNew));
			user.setId(this.getSessionUser().getId());
			int result= UserService.updateByPrimaryKeySelective(user);
			if(result>0) {
				map.put("code", "1");
				map.put("msg", "修改成功！");				
			}else {
				map.put("code", "0");
				map.put("msg", "请联系管理员！");	
			}

		}else {
			map.put("code", "0");
			map.put("msg", "历史密码输入错误！");
		}
		return map;
	}
	 
}
